package com.ytclub.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.ytclub.subject.common.enums.CategoryTypeEnum;
import com.ytclub.subject.common.enums.IsDeletedFlagEnum;
import com.ytclub.subject.domain.convert.SubjectLabelConverter;
import com.ytclub.subject.domain.entity.SubjectLabelBO;
import com.ytclub.subject.domain.service.SubjectLabelDoaminService;
import com.ytclub.subject.infra.basic.entity.SubjectCategory;
import com.ytclub.subject.infra.basic.entity.SubjectLabel;
import com.ytclub.subject.infra.basic.entity.SubjectMapping;
import com.ytclub.subject.infra.basic.service.SubjectCategoryService;
import com.ytclub.subject.infra.basic.service.SubjectLabelService;
import com.ytclub.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: HuangQi
 * @date: Created in 21:14 2023/12/14
 * @description:
 */
@Service
@Slf4j
public class SubjectLabelDoaminServiceImpl implements SubjectLabelDoaminService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;


    /**
     * 新增标签
     *
     * @param subjectLabelBO 新增标签的信息
     * @return 新增结果
     */
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDoaminServiceImpl.add.bo:{}", JSON.toJSONString(subjectLabelBO));
        }

        // 参数转换
        SubjectLabel subjectLabel =
                SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        // 调用service
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    /**
     * 更新标签
     *
     * @param subjectLabelBO 更新标签的信息
     * @return 更新结果
     */
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        // 参数转换
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDoaminServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }

        // 参数转换
        SubjectLabel subjectLabel =
                SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);

        // 调用service
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        // 参数转换
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDoaminServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }

        // 参数转换
        SubjectLabel subjectLabel =
                SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());

        // 调用service
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 根据分类查询标签
     *
     * @param subjectLabelBO 查询条件
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        // 参数转换
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDoaminServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }

        // 如果当前分类为一级分类,则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if (CategoryTypeEnum.PRIMAY.getCode() == subjectCategory.getCategoryType()) {
            // 查询所有标签
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            List<SubjectLabel> labelList = subjectLabelService.queryByCondition(subjectLabel);

            // 结果转换
            List<SubjectLabelBO> labelResultList = SubjectLabelConverter.INSTANCE.convertLabelToBoList(labelList);
            return labelResultList;
        }

        // 拿到categoryId,再去Mapping表中查询LabelId
        Long categoryId = subjectLabelBO.getCategoryId();

        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);

        // 为空直接返回
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }

        // 拿到labelId集合
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());


        // 调用service
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);

        // 结果转换
        List<SubjectLabelBO> boList = new LinkedList<>();
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setSortNum(label.getSortNum());
            bo.setCategoryId(categoryId);
            boList.add(bo);
        });

        return boList;
    }

}
